package com.config;

import com.framework.Interceptor.ServerBasePathInterceptor;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.web.accept.ContentNegotiationManager;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.resource.PathResourceResolver;
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;
import org.springframework.web.servlet.resource.ResourceResolver;
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import java.util.ArrayList;
import java.util.Arrays;

@Configuration
@EnableWebMvc
@ComponentScan({"com.controller"})
public class WebServletConfig extends WebMvcConfigurerAdapter {
    //配置InternalResourceViewResolver
    @Bean
    public InternalResourceViewResolver internalResourceViewResolver() {
        InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver( "/WEB-INF/views/", ".jsp" );
        internalResourceViewResolver.setExposeContextBeansAsAttributes( true );
        internalResourceViewResolver.setOrder( 3 );
        return internalResourceViewResolver;
    }

//    @Bean
//    public InternalResourceViewResolver hmtlInternalResourceViewResolver() {
//        InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver( "/WEB-INF/views/", ".hmtl" );
//        return internalResourceViewResolver;
//    }

    //配置configureContentNegotiation
    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        super.configureContentNegotiation( configurer );
        configurer.defaultContentType( MediaType.TEXT_HTML );
    }

    //配置ContentNegotiatingViewResolver
    @Bean
    public ContentNegotiatingViewResolver contentNegotiatingViewResolver(ContentNegotiationManager contentNegotiationManager) {
        ContentNegotiatingViewResolver contentNegotiatingViewResolver = new ContentNegotiatingViewResolver();
        contentNegotiatingViewResolver.setContentNegotiationManager( contentNegotiationManager );
        contentNegotiatingViewResolver.setOrder( 2 );
        return contentNegotiatingViewResolver;
    }

    //MultipartResolver
    //这个bean的name 必须是multipartResolver， 不然上传文件会报错 （400 Bad Request）
    @Bean
    public MultipartResolver multipartResolver() {
        CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
        commonsMultipartResolver.setDefaultEncoding( "UTF-8" );
        commonsMultipartResolver.setResolveLazily( true );//懒解析
        return commonsMultipartResolver;
    }

    @Bean
    public PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() {
        PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer();
        propertyPlaceholderConfigurer.setFileEncoding( "utf-8" );
        propertyPlaceholderConfigurer.setSearchSystemEnvironment( true );
        propertyPlaceholderConfigurer.setIgnoreUnresolvablePlaceholders( true );
        propertyPlaceholderConfigurer.setLocation( new ClassPathResource( "setting.properties" ) );
        return propertyPlaceholderConfigurer;
    }

    @Bean
    public ServerBasePathInterceptor serverBasePathInterceptor(){
        return new ServerBasePathInterceptor();
    }

    //===============================验证拦截=================
//    @Bean
//    public SpringValidatorInterceptor springValidatorInterceptor(){
//        return  new SpringValidatorInterceptor();
//    }
//
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(springValidatorInterceptor()).addPathPatterns( "/**" ).excludePathPatterns( "/assets/*" );
        registry.addInterceptor( serverBasePathInterceptor()).addPathPatterns( "/**" ).excludePathPatterns( "/assets/*" );
    }


    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

}
